// Copyright (c) 2009 Alistair Jones
// This program is made available under the terms of the MIT License.
package org.hypirinha.html.output.print;

import static org.hypirinha.html.AttributeFactory.*;
import static org.hypirinha.html.ElementFactory.*;
import org.hypirinha.html.elements.*;
import org.hypirinha.html.Text;
import static org.hypirinha.html.output.print.MultilineString.lines;
import org.hypirinha.html.output.PrintAdapter;

import org.junit.Test;
import static org.junit.Assert.assertEquals;

public class PrintAdapterTest {
    
    @Test
    public void shouldRenderElementsWithLineBreaksByDefault() {
        Div content = div().contains(div());

        String output = new PrintAdapter().asString(content);

        assertEquals(lines(
                "<div>",
                "  <div></div>",
                "</div>"), output);
    }

    @Test
    public void shouldKeepTextNodesOnTheSameLine() {
        Li content = li().contains(new Text("Hello"));

        String output = new PrintAdapter().asString(content);

        assertEquals(lines(
                "<li>Hello</li>"), output);
    }

    @Test
    public void shouldRenderChildlessElementsOnTheSameLine() {
        Input content = input(name("sheep"));

        String output = new PrintAdapter().asString(content);

        assertEquals(lines(
                "<input name=\"sheep\"></input>"), output);
    }

    @Test
    public void shouldRenderMultipleChildlessElementsIndividualLines() {
        Form content = form().contains(fieldset().contains(input(name("sheep")), input(name("sheep"))));

        String output = new PrintAdapter().asString(content);

        assertEquals(lines(
                "<form>",
                "  <fieldset>",
                "    <input name=\"sheep\"></input>",
                "    <input name=\"sheep\"></input>",
                "  </fieldset>",
                "</form>"), output);
    }

    @Test
    public void shouldIndentNestedElements() {
        Body body = body();
        body.div().ul().li();

        String output = new PrintAdapter().asString(body);

        assertEquals(lines(
                "<body>",
                "  <div>",
                "    <ul>",
                "      <li></li>",
                "    </ul>",
                "  </div>",
                "</body>"), output);
    }

    @Test
    public void shouldRenderTextNodesAndCildElementsAllOnTheSameLine() {
        Li content = li().contains(new Text("Hello"), input(name("sheep")));

        String output = new PrintAdapter().asString(content);

        assertEquals(lines(
                "<li>Hello<input name=\"sheep\"></input></li>"), output);
    }

    @Test
    public void shouldEscapeAttributesAndTextNodesSoThatDocumentStructureIsPreserved() {
        Html content = html();
        content.head().title(id("\"quoted attribute\"")).text("< less than & greater than >");

        String output = new PrintAdapter().asString(content);

        assertEquals(lines(
                "<html>",
                "  <head>",
                "    <title id=\"&quot;quoted attribute&quot;\">&lt; less than &amp; greater than &gt;</title>",
                "  </head>",
                "</html>"), output);
    }
}
